<?php
/**
 * Created by PhpStorm .
 * User: {Administrator}
 * Date: {2022/7/8 18:02}
 * Author: '树叶'
 * Email: 'ayunu@qq.com'
 * Motto Info: '理想如果不向现实做一点点屈服，那么理想也将归于尘土。'
 */

namespace app\api\model;
use think\Db;
use think\Model;
use app\admin\common;
class Vehicle extends Model
{
    // 开启自动写入时间戳字段
        protected $autoWriteTimestamp = 'int';
        // 定义时间戳字段名
        protected $createTime = 'createtime';
        protected $updateTime = 'updatetime';
        protected $resultSetType = 'collection';
        // 追加属性
        protected $append = [

        ];

        public function getVehicle($ids = '')
        {
            $result = [
                'data' => [],
                'status' => false,
                'msg'   => ''
            ];
            if (!$ids) return $result;

            $where = [];
            $where['id'] = ['eq',$ids];
            $res = Db::name('vehicle')->field('id,pid,first,mergename')->where("del",0)->where('id','in',$ids)->select();

            $result['msg'] = '参数错误';
            if (!$res) return $result;
            // 按字母排序
            $sort = array();
            foreach($res as $k=>$v)
            {
                $py = zhcnToPinyin($v['first']);
                $list[$k]['pinyin'] = $py;
                $sort[] = $py;
            }

            array_multisort($sort, SORT_ASC, $res);
            $result['data'] = $res;
            $result['msg'] = '获取成功';
            $result['status'] = true;
            return $result;
        }

    public function models($uid = '', $data = [])
    {
        $array = [];
        $level = 0;
        $pid   = 0;
        $num   = 0;
        if (count($data['title']) > 5)
        {
            return false;
        }

        $data['title'] = array_values($data['title']);
        $pids = '0';
        foreach($data['title'] as $key => $val)
        {
            $label = Db::name('vehicle')->where('mergename',$val)->find();
            $num += 1;
            if ($num == 3)
            {
                $label = Db::name('vehicle')
                    ->where('pid',$pid)
                    ->where("del",0)
                    ->select();
                $val = substr($val, 0, 4);
                foreach ($label as $la_key => $la_val)
                {
                    $mergename = explode('-',$la_val['mergename']);
                    if (empty($mergename[1]))
                    {
                        if ($mergename[0] == $val)
                        {
                            $label = $la_val;
                            continue;
                        }
                    }elseif ($mergename[0] <= $val && $mergename[1] >= $val)
                    {
                        $label = $la_val;
                        continue;
                    }else{
                        $label = false;
                    }
                }
            }
            $level   += 1;
            if (empty($label) && $level == 1)
            {
                $pids = '0';
            }

            if ($label)
            {
                $pids    = $label['pids'];
                $pid     = $label['id'];
                $array[] = $pid;
                continue;
            }else{
                $pids    = $pids.'-'.$pid;
            }

            $insert = [
                'pid'       => $pid,
                'mergename' => $val,
                'level'     => $level,
                'first'     => empty($label) ? 'Z' :$label['first'],
                'pids'      => $pids,
                'create_time' => time()
            ];
            $pid = Db::name('vehicle')->insertGetId($insert);
            $array[] = $pid;
        }


            $user = Db::name('user')->where('id',$uid)->find();
            if ($user)
            {
                $carId = Db::name('user_car')->insertGetId([
                    'uid'    => $uid,
                    'name'   => $user['nickname'],
                    'brand'  => $array[0],
                    'models' => $array[1],
                    'year'   => $array[2],
                    'engine' => $array[3],
                    'label'  => $array[4],
                    'create_time'  => time(),
                ]);
                return $carId;
            }else{
                return $data['title'];
            }

    }


}